//
//  ZWComposePictureView.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/27.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit
import SVProgressHUD

class ZWComposePictureView: UICollectionView {
    var images:[UIImage]? = []
    var pickImageClosure: (()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }
  
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        registerClass(ZWComposeViewCell.self, forCellWithReuseIdentifier: "cell")
        delegate = self
        dataSource = self
        
        
    }
    //给外界提供一个设置图片的方法
    func setImage(image:UIImage){
        if images?.count < 9 {
            images?.append(image)
            reloadData()
        }else{
            SVProgressHUD.showInfoWithStatus("图片已满")
        }
    }
    //设置item大小 和 layout的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let margin: CGFloat = 5
        let itemWH = (self.frame.width - 2 * margin)/3
        // 设置layout的itemSize的大小
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
    }
   
}

// MARK: - collectionView的代理
extension ZWComposePictureView:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.hidden = images?.count == 0
        return images!.count + ((images?.count == 0 || images?.count == 9) ? 0 : 1)
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ZWComposeViewCell
        cell.deleteBTNClosure = {[weak self] in
            self!.images?.removeAtIndex(indexPath.item)
            self!.reloadData()
        }
        if indexPath.item == images?.count {
            cell.deleteBTN.hidden = true
            cell.imageView.image = UIImage(named: "compose_pic_add")
        }else{
            cell.deleteBTN.hidden = false
            cell.imageView.image = images![indexPath.item]
        }
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if indexPath.item == images?.count {
            pickImageClosure!()
        }
    }
}

// MARK: - cell
class ZWComposeViewCell: UICollectionViewCell{
    var deleteBTNClosure: (()->())?
    
    @objc private func deleteButtonClick(button:UIButton){
        deleteBTNClosure!()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(imageView)
        
        contentView.addSubview(deleteBTN)
        
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        deleteBTN.snp_makeConstraints { (make) in
            make.top.trailing.equalTo(contentView)
        }
    }
    
    //懒加载控件
    private lazy var imageView:UIImageView = {
        let view = UIImageView()
        
        return view
    }()
    
    private lazy var deleteBTN:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(ZWComposeViewCell.deleteButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        return button
    }()

}